package com.cci.kangdao.service.impl;


import com.cci.kangdao.aop.UserThreadLocal;
import com.cci.kangdao.dao.FaultCodeQueryFeedBackRecordDao;
import com.cci.kangdao.dao.model.FaultCodeQueryFeedBackRecordT;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.dto.request.ClickLikeOrStepOnRequest;
import com.cci.kangdao.service.FaultCodeQueryFeedBackRecordTService;
import com.cci.kangdao.utilTool.*;
import com.gexin.fastjson.JSON;
import com.gexin.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Service("FaultCodeQueryFeedBackRecordTService")
public class FaultCodeQueryFeedBackRecordTServiceImpl implements FaultCodeQueryFeedBackRecordTService {

	private Logger log = Logger.getLogger(this.getClass().getName());

	@Resource
	private FaultCodeQueryFeedBackRecordDao faultCodeQueryFeedBackRecordDao;

	@Override
	@Transactional
	public ModelAndView clickLikeOrStepOn(String paramStr, HttpServletRequest request,
								  HttpServletResponse response) {
		log.info("clickLikeOrStepOn接口入参paramStr："+paramStr);
		JSONObject jsonObject = new JSONObject();
		//1.提取参数信息
		ClickLikeOrStepOnRequest clickLikeOrStepOnRequest = JSON.parseObject(paramStr, ClickLikeOrStepOnRequest.class);
		//2.校验参数是否必填
		Integer type = clickLikeOrStepOnRequest.getType();
		String faultCode = clickLikeOrStepOnRequest.getFaultCode();
		String spn = clickLikeOrStepOnRequest.getSPN();
		String fmi = clickLikeOrStepOnRequest.getFMI();
		String feedBackMsg = clickLikeOrStepOnRequest.getFeedBackMsg();
		if(type == -1 || StringUtils.isEmpty(faultCode) || StringUtils.isEmpty(spn) || StringUtils.isEmpty(fmi)
		|| (type == 2 && StringUtils.isEmpty(feedBackMsg))){
			ParameterTool.writeResponse(response, 17,"缺少必要参数或所传参数非枚举值！");
			return null;
		}
		String currentTime = LocalTimeUtil.getRealCurrentTime();
		UserT userT = UserThreadLocal.get();
		FaultCodeQueryFeedBackRecordT faultCodeQueryFeedBackRecordT = new FaultCodeQueryFeedBackRecordT();
		faultCodeQueryFeedBackRecordT.setUserId(userT.getId());
		faultCodeQueryFeedBackRecordT.setFaultCode(faultCode);
		faultCodeQueryFeedBackRecordT.setFmi(fmi);
		faultCodeQueryFeedBackRecordT.setSpn(spn);
		faultCodeQueryFeedBackRecordT.setType(type);
		faultCodeQueryFeedBackRecordT.setFeedBackMsg(feedBackMsg);
		faultCodeQueryFeedBackRecordT.setFeedBackTime(currentTime);
		faultCodeQueryFeedBackRecordT.setCreator(userT.getId());
		faultCodeQueryFeedBackRecordT.setCreateTime(currentTime);
		faultCodeQueryFeedBackRecordDao.insertSelective(faultCodeQueryFeedBackRecordT);
		jsonObject.put("data","");
		jsonObject.put("status",0);
		jsonObject.put("msg","success");
		ParameterTool.writeResponse(response, jsonObject.toString());
		return null;
	}

}
